// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <pollux/functions/prestosql/benchmarks/ArrayMinMaxBenchmark.h>

#include <pollux/benchmarks/ExpressionBenchmarkBuilder.h>
#include <pollux/common/base/pollux_exception.h>
#include <pollux/functions/registerer.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>

namespace kumo::pollux::functions {

void registerTestVectorFunctionBasic() {
  POLLUX_REGISTER_VECTOR_FUNCTION(udf_array_min_basic, "vector_basic");
}

void registerTestSimpleFunctions() {
  register_function<ArrayMinSimpleFunction, int32_t, Array<int32_t>>(
      {"array_min_simple"});
  register_function<ArrayMinSimpleFunctionIterator, int32_t, Array<int32_t>>(
      {"array_min_simple_iterator"});

  register_function<
      ArrayMinSimpleFunctionSkipNullIterator,
      int32_t,
      Array<int32_t>>({"array_min_simple_skip_null_iterator"});
}
} // namespace kumo::pollux::functions

using namespace kumo::pollux;

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};
  functions::prestosql::registerArrayFunctions();

  functions::registerTestVectorFunctionBasic();

  functions::registerTestSimpleFunctions();
  ExpressionBenchmarkBuilder benchmarkBuilder;
  auto inputType = ROW({"c0"}, {ARRAY(INTEGER())});

  benchmarkBuilder.addBenchmarkSet("array_min_max", inputType)
      .withIterations(1000)
      .addExpression("vector_basic", "vector_basic(c0)")
      .addExpression(
          "simple_skip_null_iterator",
          "array_min_simple_skip_null_iterator(c0)")
      .addExpression("simple_iterator", "array_min_simple_iterator(c0)")
      .addExpression("simple", "array_min_simple(c0)")
      .addExpression("prestoSQLArrayMin", "array_min(c0)");

  benchmarkBuilder.registerBenchmarks();

  // Make sure all expressions within benchmarkSets have the same results.
  benchmarkBuilder.testBenchmarks();

  melon::runBenchmarks();

  return 0;
}
